package com.weixing.mall.provider.controller.admin;

import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.SysLog;
import com.weixing.mall.core.enums.OperateEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.api.model.vo.AgentBonusRuleVo;
import com.weixing.mall.provider.model.domain.AgentBonusRule;
import com.weixing.mall.provider.model.dto.AgentBonusRuleDto;
import com.weixing.mall.provider.service.IAgentBonusRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 代理分红规则接口
 *
 * @author siashan
 * @since  v1.0
 */
@RestController
@RequestMapping("agent/bonusRule")
public class AgentBonusRuleController extends BaseController {
    @Autowired
    private IAgentBonusRuleService agentBonusRuleService;

    /**
     * 新增
     *
     * @param dto  参数
     * @return     结果
     */
    @PostMapping("")
    @SysLog(type = OperateEnum.S, desp = "编辑代理分红规则")
    public Result save(AgentBonusRuleDto dto){
        if (null == dto.getId()){
            String ruleName = dto.getRuleName();
            AgentBonusRule bonusRule = agentBonusRuleService.selectByRuleName(ruleName);
            if (null != bonusRule){
                return ResultUtil.error("已存在同名规则");
            }
        }
        agentBonusRuleService.save(dto);
        return ResultUtil.success(dto);
    }

    /**
     * 查看详情
     *
     * @param id  id
     * @return    详情
     */
    @PostMapping("/{id}")
    public Result<AgentBonusRule> selectDetail(@PathVariable long id){
        return ResultUtil.success(agentBonusRuleService.getById(id));
    }

    /**
     * 查询列表
     *
     * @return   列表
     */
    @GetMapping("")
    public Result<List<AgentBonusRuleVo>> selectList(){
        return ResultUtil.success(agentBonusRuleService.selectListOfPage());
    }
}
